<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>

<script>
    function merge(arr, lo, hi, mid) {
      //对左右两边有序的数组按大小进行归并
      let tmp_arr =[];
      for (let i = lo; i <= hi; i ++) {
        tmp_arr[i] = arr[i];
      }
      let i = lo;
      let l = lo;
      let r = mid + 1;
      //从左边开始取
      while (i <= hi) {
        if (l > mid) {
          arr[i] = tmp_arr[r ++];
        }
        else if (r > hi) {
          arr[i] = tmp_arr[l ++];
        }
        else if (tmp_arr[r] < tmp_arr[l]) {
          arr[i] = tmp_arr[r ++];
        }
        else{
          arr[i] = tmp_arr[l ++];
        }
        i ++;
      }
      return arr;
    }

    function sort(arr, lo, hi) {
      if (hi <= lo) return ;
      let mid = lo + (hi - lo) / 2;
      mid = parseInt(mid);
      // 排左半部分
      sort(arr, lo, mid);
      // 排右半部分
      sort(arr, mid + 1, hi);
      merge(arr, lo, hi, mid);
    }


    test_arr = [1, 12, 8, 7, 9, 22, 4, 632, 81, 101];
    let rs = sort(test_arr, 0, test_arr.length - 1);
    console.log(test_arr);


</script>


</html>